*****************************************************************************
*** prod_laundet.do
***
*****************************************************************************

*** Load product-specific information and read size
use Original_Data\prod_laundet, clear

gen lastword = word(l9,-1)
gen oz = regexm(lastword,"OZ")
tab oz
gen int size = real(subinstr(lastword,"OZ","",1)) /*Explain why*/
gen product = rtrim(subinstr(l9,lastword,"",1))



gen brand=l5

replace brand="123" if l5=="1 2 3 EXTRA" | l5=="123 FUERZA" | l5=="123 FUERZA AZUL"
replace brand="AJAX" if l5=="AJAX FRESH" | l5=="AJAX"
replace brand="ALL" if l5=="ALL" | l5=="ALL BABY CARE PLUS" | l5=="ALL SMALL & MIGHTY" | l5=="ALL SURF"
replace brand="ARM & HAMMER" if l5=="ARM & HAMMER" | l5=="ARM & HAMMER COMPLETE" | l5=="ARM & HAMMER FABRICARE"
replace brand="CHEER" if l5=="CHEER" | l5=="CHEER TRUE FIT"
replace brand="FRESH" if l5=="FRESH BREEZE" | l5=="FRESH START"
replace brand="GAIN" if l5=="GAIN JOYFUL EXPRESSIONS" | l5=="GAIN PLUS SOFTNESS" 
replace brand="KLEEN BRITE" if l5=="KLEEN BRITE" | l5=="KLEEN BRITE BRIGHT WATER" 
replace brand="OXI CLEAN" if l5=="OXI CLEAN" | l5=="OXI CLEAN TOSS N GO"
replace brand="PUREX" if l5=="PUREX" | l5=="PUREX ADVANCED" | l5=="PUREX BABY" | l5=="BABY SOFT PUREX" 
replace brand="SPRING" if l5=="SPRING AIRE" | l5=="SPRING DAY" | l5=="SPRING TIME"
replace brand="SUN" if l5=="SUN" | l5=="SUN BURST" | l5=="SUN CUDDLE SOFT"
replace brand="TIDE" if l5=="TIDE" | l5=="TIDE KICK" | l5=="TIDE SIMPLE PLEASURES"
replace brand="WISK" if l5=="WISK" | l5=="WISK POWER SCOOP"
replace brand="XTRA" if l5=="XTRA" | l5=="XTRA WASH 'N GO SINGLES"
codebook brand

*** DROP products if product type is in list {cleaning kit, dispenser, laundry care kit, missing, mixed product}
encode producttype, g(ProdType)
tab ProdType
tab ProdType, nol
drop if ProdType==1 | ProdType==3 | ProdType==4 | ProdType==6 | ProdType==7
drop ProdType
encode producttype, g(TypeProd)
tab TypeProd, nol

gen DetFabSoft=(TypeProd==1)
***** manipulate additives. It is a string variable so cannot use recode to change Missing to dot****

*** this method generates "product-  " if additives is missing   
* destring additives, generate(addit_num)/* does not work, it's inverse is tostring*/
encode additives, generate(additnum)
codebook additnum if additives=="MISSING" /* oorresponds to number 19*/
mvdecode additnum, mv(19=.) /*additnum: 1518 missing values generated */
decode additnum, g(additives1)
capture drop product1

encode typeofformula, gen (formula)
codebook formula
codebook formula if typeofformula=="MISSING" /* corresponds to number 13*/
mvdecode formula, mv (13=.)
decode formula, gen (formula1)

tab package form /* usually liquid is in plastic bottle
and powder in box or plastic bag*/
capture tab package size
encode form, gen(form1)
codebook form1 if form=="MISSING"
mvdecode form1, mv(3=.)
drop form
decode form1, gen (form)
gen product1=product+" - "+(additives1)+" - "+(formula1)+" - "+(form)
encode product1, gen(prodcode)
codebook prodcode

replace product=product1

sort product1 size
l lastword size if prodcode==54 /*this is the one with the weird sizes in year 2001. Stata
does something weird with the size when generating unique id*/


keep upc size product1 prodcode package brand DetFabSoft
label variable package "package type"
label variable product1 "product description, additives, formula and form"

sort upc
isid upc product1

encode package, gen(packagecode)
egen sdpackagecode=sd(packagecode), by (prodcode)
egen sdpackagecode_size=sd(packagecode), by (prodcode size)
label var sdpackagecode "if greater than 0 then a product line comes in different packages"
label variable sdpackagecode_size "if greater than 0 then a specific size comes in different packages"
gen sdpackagecode_size_dum=sdpackagecode_size>0 & sdpackagecode_size!=.
label var sdpackagecode_size_dum "1 if a pack comes in different packages in general (prod_laundet)"

sort prodcode
l product1 prodcode size package if sdpackagecode_size_dum==1, nol


